package com.cykj.service.impl;

import com.cykj.mapper.UserMapper;
import com.cykj.pojo.Admin;
import com.cykj.pojo.TicketsOrder;
import com.cykj.pojo.User;
import com.cykj.service.UserService;
import com.cykj.util.PageResult;
import com.cykj.util.PageUtils;
import com.cykj.util.ResponseDto;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public ResponseDto deleteUser(int userId) {
        int i = userMapper.deleteUser(userId);
        if (i == 0){
            return new ResponseDto(0,"删除失败",0);
        }
        return new ResponseDto(1,"删除成功",null);
    }

    @Override
    public ResponseDto selectAgeCount(List<Integer> ages) {
        List<Integer> countAge = new ArrayList<>();
        countAge.add(0);
        for (int i = 0; i < ages.size()-1; i++) {
            int age1 = ages.get(i);
            int age2 = ages.get(i+1);
            int count = userMapper.selectAgeCount(age1, age2);
            countAge.add(count);
        }

        return new ResponseDto(1,"年龄段查询成功",countAge);
    }

    @Override
    public ResponseDto selectByData(int pageCurr, int pageSize,String value) {
        Page<Object> page = PageHelper.startPage(pageCurr, pageSize);
        List<User> ticketsOrders = userMapper.selectByData(value);
        PageInfo<Object> pageInfo = new PageInfo<>(page);
        PageResult pageResult = PageUtils.getPageResult(pageInfo);
        return new ResponseDto(1,"查询成功",pageResult);
    }

    @Override
    public ResponseDto login(String name, String pwd) {
        User login = userMapper.login(name, pwd);
        if (login == null){
            return new ResponseDto(0,"失败",null);
        }else {
            return new ResponseDto(1,"登录成功",login);
        }
    }

    @Override
    public ResponseDto register(String name, String pwd,String age,String sex) {
        int register = userMapper.register(name, pwd,age,sex);
        if (register == 0){
            return new ResponseDto(0,"注册失败",null);
        }else {
            return new ResponseDto(1,"注册成功",null);
        }
    }

    @Override
    public ResponseDto alterPwd(int id, String newPwd) {
        int i = userMapper.alterPwd(id, newPwd);
        if (i == 1) {
            return new ResponseDto(1,"修改成功",i);
        }else {
            return new ResponseDto(0,"修改失败",null);
        }
    }
}
